package com.gensdai.leliang.common.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.common.clickinterface.OnProductClickListener;
import com.gensdai.leliang.entity.parseBean.ProductInfo;

import butterknife.BindView;
import butterknife.ButterKnife;

/**首页热卖大图适配
 * Created by LT on 2017/6/15.
 */

public class ContentHotBigPicAdapterRec extends DelegateAdapter.Adapter<ContentHotBigPicAdapterRec.ContentTitleViewHolder> {

    private Context mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    int count = 1;

    ProductInfo bean;

    OnProductClickListener click;

    public ContentHotBigPicAdapterRec(Context context, LayoutHelper layoutHelper, ProductInfo bean) {
        this(context, layoutHelper, bean, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public ContentHotBigPicAdapterRec(Context context, LayoutHelper layoutHelper, ProductInfo bean, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.bean = bean;
        this.mLayoutParams = layoutParams;
    }

    public void setOnProductClicklistener(OnProductClickListener click){
        this.click = click;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public ContentTitleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ContentTitleViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_main_hot_big_pic, parent, false));
    }

    @Override
    public void onBindViewHolder(ContentTitleViewHolder holder, final int position) {
        holder.itemView.setLayoutParams(
                new VirtualLayoutManager.LayoutParams(mLayoutParams));

        if (bean != null && !TextUtils.isEmpty(bean.getLogo())) {
            holder.bigPic.setImageURI(bean.getLogo());
            holder.bigPic.setBackgroundResource(R.color.image_placebg_color3);
//            Uri uri = Uri.parse(bean.getLogo());
//            int width = 100, height = 100;
//            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
//                    .setResizeOptions(new ResizeOptions(width, height))
//                    .build();
//            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
//                    .setOldController(holder.bigPic.getController())
//                    .setImageRequest(request)
//                    .build();
//            holder.bigPic.setController(controller);

            holder.bigPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(click != null){
                        click.OnProductClick(bean,position);
                    }
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return count;
    }


    static class ContentTitleViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.bigPic)
        SimpleDraweeView bigPic;

        public ContentTitleViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }

}
